package com.tiger.project.service.impl;

import java.util.Arrays;
import java.util.List;

import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.CommonDecision;
import com.tiger.project.domain.ContractBill;
import com.tiger.project.domain.ContractSponsorCredit;
import com.tiger.project.mapper.CommonDecisionMapper;
import com.tiger.project.mapper.ContractBillMapper;
import com.tiger.project.mapper.ContractSponsorCreditMapper;
import com.tiger.project.service.IContractSponsorCreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 租金减免Service业务层处理
 *
 * @author standard
 * @date 2022-11-09
 */
@Service
public class ContractSponsorCreditServiceImpl implements IContractSponsorCreditService {
    @Autowired
    private ContractSponsorCreditMapper contractSponsorCreditMapper;
    @Autowired
    private CommonDecisionMapper decisionMapper;
    @Autowired
    private ContractBillMapper billMapper;

    /**
     * 查询租金减免
     *
     * @param id 租金减免主键
     * @return 租金减免
     */
    @Override
    public ContractSponsorCredit selectContractSponsorCreditById(Long id) {
        ContractSponsorCredit contractSponsorCredit = contractSponsorCreditMapper.selectContractSponsorCreditById(id);
        //决策依据
        String decisionId = contractSponsorCredit.getDecisionId();
        if (StringUtils.isNotEmpty(decisionId)){
            List<String> ids = Arrays.asList(decisionId.split(","));
            for (String i : ids) {
                CommonDecision decision = decisionMapper.selectCommonDecisionById(Long.valueOf(i));
                contractSponsorCredit.getCommonDecisionList().add(decision);
            }
        }
        //开票信息
        contractSponsorCredit.setContractBill(billMapper.selectContractBillById(contractSponsorCredit.getBillId()));
        return contractSponsorCredit;
    }

    /**
     * 查询租金减免列表
     *
     * @param contractSponsorCredit 租金减免
     * @return 租金减免
     */
    @Override
    public List<ContractSponsorCredit> selectContractSponsorCreditList(ContractSponsorCredit contractSponsorCredit) {
        List<ContractSponsorCredit> creditList = contractSponsorCreditMapper.selectContractSponsorCreditList(contractSponsorCredit);
        //获取账单对象
        for (ContractSponsorCredit c : creditList){
            c.setContractBill(billMapper.selectContractBillById(c.getBillId()));
        }
        return creditList;
    }

    /**
     * 新增租金减免
     *
     * @param contractSponsorCredit 租金减免
     * @return 结果
     */
    @Override
    public int insertContractSponsorCredit(ContractSponsorCredit contractSponsorCredit) {
        return contractSponsorCreditMapper.insertContractSponsorCredit(contractSponsorCredit);
    }

    /**
     * 修改租金减免
     *
     * @param contractSponsorCredit 租金减免
     * @return 结果
     */
    @Override
    public int updateContractSponsorCredit(ContractSponsorCredit contractSponsorCredit) {
        return contractSponsorCreditMapper.updateContractSponsorCredit(contractSponsorCredit);
    }

    /**
     * 批量删除租金减免
     *
     * @param ids 需要删除的租金减免主键
     * @return 结果
     */
    @Override
    public int deleteContractSponsorCreditByIds(Long[] ids) {
        return contractSponsorCreditMapper.deleteContractSponsorCreditByIds(ids);
    }

    /**
     * 删除租金减免信息
     *
     * @param id 租金减免主键
     * @return 结果
     */
    @Override
    public int deleteContractSponsorCreditById(Long id) {
        return contractSponsorCreditMapper.deleteContractSponsorCreditById(id);
    }

    /**
     *
     * @param id 合同发起id
     * @return
     */
    @Override
    public List<Integer> selectRentPeriods(Long id) {
        return contractSponsorCreditMapper.selectRentPeriods(id);
    }

    /**
     * 开票信息
     * @param contractSponsorId
     * @param rentPeriods
     * @return
     */
    @Override
    public ContractBill makeInvoice(Long contractSponsorId, Long rentPeriods) {
        return billMapper.makeInvoice(contractSponsorId, rentPeriods);
    }

    /**
     *
     * @param id 租金减免id
     * @param status 审批状态
     * @param oaFile OA文件链接
     * @return
     */
    @Override
    public int callBack(Long id, Long status, String oaFile) {
        ContractSponsorCredit sponsorCredit = contractSponsorCreditMapper.selectContractSponsorCreditById(id);
        //租金减免审核通过之后，对应账期租金减免金额累计
        if (StringUtils.isNotNull(status) && 3L == status){
            billMapper.addCreditAmount(sponsorCredit.getBillId(),sponsorCredit.getCreditAmount());
        }
        ContractSponsorCredit credit = new ContractSponsorCredit();
        credit.setId(id);
        credit.setStatus(status);
        credit.setOaFile(oaFile);
        return contractSponsorCreditMapper.updateContractSponsorCredit(credit);
    }
}
